<template>
  <div class="app-container">
    <el-card class="box-card" v-if="pageType == 1">
      <!-- 搜索区域 -->
      <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="100px">
        <el-form-item label="入库编号" prop="warehouseNumber">
          <el-input v-model="queryParams.warehouseNumber" placeholder="请输入入库单号" clearable size="small" />
        </el-form-item>
        <el-form-item label="供应商" prop="merId">
          <el-select v-model="queryParams.merId" placeholder="请选择供应商" clearable size="small">
              <el-option v-for="item in supplierList" :key="item.id" :label="item.name" :value="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="入库状态" prop="inOrOutWarehouseStatus">
          <el-select v-model="queryParams.inOrOutWarehouseStatus" placeholder="请选择入库状态" clearable size="small">
            <el-option label="待入库" value="0" />
            <el-option label="已入库" value="1" />
            <el-option label="已取消" value="2" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">查询</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <!-- 操作按钮区域 -->
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd">新增入库单</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button type="primary" plain size="mini" @click="handleBatchImport">批量导出</el-button>
        </el-col>
      </el-row>

      <!-- 数据表格 -->
      <el-table v-loading="loading" :data="tableData">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="入库单号" align="center" prop="inOrOutOrderNumber" />
        <el-table-column label="供应商" align="center" prop="transferOutRoomWarehouseName" />
        <el-table-column label="入库仓库" align="center" prop="transferInRoomWarehouseName" />
        <el-table-column label="商品总数量" align="center" prop="totalNumber" />
        <el-table-column label="商品总金额" align="center" prop="totalPrice" />
        <el-table-column label="创建人" align="center" prop="creator" />
        <el-table-column label="入库状态" align="center" prop="inOrOutWarehouseStatus">
          <template slot-scope="scope">
            <el-tag :type="getStatusType(scope.row.inOrOutWarehouseStatus)">{{ getStatusText(scope.row.inOrOutWarehouseType,scope.row.inOrOutWarehouseStatus) }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="创建时间" align="center" prop="createTime" width="180" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button type="text" size="mini" @click="handleDetail(scope.row)">详情</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <div class="pagination-container">
        <div class="left">共 {{ total }} 项数据</div>
        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
          :key="getRandomNumber()"
        />
      </div>
    </el-card>
    <el-card class="box-card" v-else-if="pageType == 3">
      <select-product @handleBack="handleBack" :defaultTransferInType="defaultTransferInType" sendType="采购入库" :defaultTransferOutType="defaultTransferOutType"></select-product>
    </el-card>
    <el-card class="box-card" v-else-if="pageType == 2">
      <purchase-receipt-detail 
        :detail-data="detailData"
        :detail-row="detailRow"
        @back="handleBack"
        @approve="handleApprove"
      />
    </el-card>
  </div>
</template>

<script>
import { listProductSpec, selectPurchaseProductAttrValueList } from '@/api/commercial';
import PurchaseReceiptDetail from './detail.vue';
import { storeHouseMixin } from '@/mixins/storeHouse';
import { getPurchaseInWarehouseFormList, getPurchaseInWarehouseFormInfo } from '@/api/storehouse';
import { merchantListApi } from '@/api/merchant';

export default {
  name: 'PurchaseReceipt',
  mixins: [storeHouseMixin],
  components: {
    PurchaseReceiptDetail
  },
  data() {
    return {
      // 遮罩层
      loading: false,
      // 总条数
      total: 0,
      // 表格数据
      tableData: [],
      // 查询参数
      queryParams: {
        page: 1,
        limit: 10,
        receiptNumber: '',
        merId: '',
        warehouse: '',
        inOrOutWarehouseStatus: ''
      },
      defaultTransferOutType: 0,
      defaultTransferInType: 3,  // 0: 供应商 1: 门仓 2: 门店 3：仓库
      detailData: [],
      supplierList: [],
      detailRow: {}
    }
  },
  created() {
    this.getList()
    this.getSupplierList()
  },
  methods: {
     getSupplierList() {
      merchantListApi({ page: 1, limit: 999 }).then((res) => {
        this.supplierList = res.list;
      });
    },
    /** 查询列表 */
    getList() {
      this.loading = true
      // TODO: 调用接口获取数据
      getPurchaseInWarehouseFormList(this.queryParams).then(res => {
        // console.log(res);
        this.tableData = res.list
        this.total = res.total
        this.loading = false
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.page = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    /** 新增按钮操作 */
    handleAdd() {
      // this.isAdd = true
      this.pageType = 3
    },
    /** 批量导出按钮操作 */
    handleBatchImport() {
      // TODO: 实现批量导出逻辑
    },
    /** 查看详情按钮操作 */
    handleDetail(row) {
      console.log(row)
      this.detailRow = row
      // TODO: 实现查看详情逻辑
      getPurchaseInWarehouseFormInfo({id: row.id}).then(res => {
        console.log(res)
        this.detailData = res
      })
      this.pageType = 2
    },
    /** 状态显示 */
    getStatusType(status) {
      if (status == '0') return 'warning'
      if (status == '1') return 'success'
      if (status == '2') return 'danger'
      return ''
    },
    /** 获取状态文本 */
    getStatusText(type=2,status) {
      // 0:待出库(待入库) 1:已出库(已入库) 2:已取消
      const statusMap = {
        1: {
          0: "待出库",
          1: "已出库",
          2: "已取消",
        },
        2: {
          0: "待入库",
          1: "已入库",
          2: "已取消",
        },
      };
      return (
        statusMap[type][
          status
        ] || "未知状态"
      );
    },
    // 每页数量改变
    handleSizeChange(val) {
      this.queryParams.limit = val
      this.getList()
    },
    // 当前页改变
    handleCurrentChange(val) {
      this.queryParams.page = val
      this.getList()
    }
  }
}
</script>

<style lang="scss" scoped>
.app-container {
  padding: 20px;
  
  .pagination-container {
    margin-top: 20px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    
    .left {
      color: #606266;
      font-size: 13px;
    }
  }
}
</style> 